package com.zhidisoft.manage.springmvccontroller;

import com.zhidisoft.manage.dao.TaxPayerDao;
import net.sf.json.JSONArray;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * The type List tax payer spring mvc.
 */
@Controller
public class ListTaxPayerSpringMvc {


    /**
     * 到展示纳税人页面
     *
     * @return the string
     */
    @RequestMapping("/tolistTaxPayer.do")
    public String toListTaxPayer(){
        return "listTaxPayer";
    }

    /**
     * 展示纳税人.
     *
     * @param page      the page
     * @param rows      the rows
     * @param payerName the payer name
     * @param payerCode the payer code
     * @return the map
     */
    @RequestMapping("/listTaxPayer.do")
    @ResponseBody
    public Map ListTaxPayer(String page,String rows,String payerName,String payerCode){
        int pageNum = 1;
        if (page != null && !page.isEmpty()) {
            pageNum = Integer.parseInt(page);
        }
        int pageSize = 10;
        if (rows != null && !rows.isEmpty()){
            pageSize = Integer.parseInt(rows);
        }
        TaxPayerDao tpd = new TaxPayerDao();
        List<Map<String, String>> result =
                tpd.getSearchResult(pageNum, pageSize, payerCode, payerName);
        JSONArray jsonArray = new JSONArray();
        for (Map map:result) {
            jsonArray.add(map);

        }
        int totalRows = tpd.getSearchRows(payerCode, payerName);
        ModelMap map = new ModelMap();
        map.put("total",totalRows);
        map.put("rows",jsonArray);
        return map;
    }
}
